package org.grow.auth.config;

import lombok.NoArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

/**
 * @Author: xwg
 * @CreateDate: 2022/2/11
 */

@EnableWebSecurity
//@EnableRedisHttpSession
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        super.configure(http);
        http.cors().disable();
        http.csrf().disable();
        http.httpBasic().disable();

    }


    @Bean
    public PasswordEncoder passwordEncoder(){
//          开发完毕使用：
//        return new BCryptPasswordEncoder();
//        开发过程使用：
        return NoOpPasswordEncoder.getInstance();
    }
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        DaoAuthenticationProvider daoAp = new DaoAuthenticationProvider();
        daoAp.setPasswordEncoder(passwordEncoder());
//        daoAp.setUserDetailsService();


//        不定参数可以加入多个ap
        ProviderManager pm = new ProviderManager(daoAp);
        return pm;
    }

    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }


}
